package pers.wq.model.dao.core;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

import org.hibernate.Session;
import org.hibernate.Transaction;

import pers.wq.model.bean.Student;

public class BeanDao {
	public static boolean save(Object obj) {
		boolean reb = true;
		try {
			if (obj instanceof Student)  {
				Student stu = (Student)obj;
				StringBuilder sql = new StringBuilder("INSERT INTO student(sname,gender,mid,entry_time,password)");
				String b = stu.getMajor().getMid().equals("null")?"null":stu.getMajor().getMid().equals("0")?"null":stu.getMajor().getMid();
				sql.append(" VALUES ('"+stu.getSname()+"','"+stu.getGender()+"',"+b+","+stu.getEntry_time()+",'"+stu.getPassword()+"')");
				System.out.println("BeanDao if in sql = " + sql.toString() + "][" + b);
				QueryDao.query(sql.toString());
			}else {
				Session session = HibernateUtil.getSession();
				Transaction tx = session.beginTransaction();
				session.save(obj);
				tx.commit();
				session.close();
			}
		}catch(Exception e) {
			reb = false;
		}
		return reb;
	}
	public static boolean save(Object obj[]) {
		boolean reb = true;
		try {
			Session session = HibernateUtil.getSession();
			Transaction tx = session.beginTransaction();
			for (Object  object: obj) {
				session.save(object);
			}
			tx.commit();
			session.close();
		}catch(Exception e) {
			reb = false;
		}
		return reb;
	}
	
	public static Object select(Class c,int id) {
		Object reObj = null;
		try {
			Session session = HibernateUtil.getSession();
			Transaction tx = session.beginTransaction();
			reObj = session.get(c, id);
			tx.commit();
			session.close();
		}catch(Exception e) {
			reObj = null;
		}
		return reObj;
	}
	
	public static boolean delete(Object obj) {
		boolean reb = true;
		try {
			Session session = HibernateUtil.getSession();
			Transaction tx = session.beginTransaction();
			session.delete(obj);
			tx.commit();
			//session.flush();
			session.close();
		}catch(org.hibernate.exception.ConstraintViolationException e) {
			System.out.println("BeanDao try catch " + e.getMessage());
			reb = false;
		}
		System.out.println("BeanDao over " + reb);
		return reb;
	}
	public static boolean delete(Object[] obj) {
		boolean reb = true;
		try {
			Session session = HibernateUtil.getSession();
			Transaction tx = session.beginTransaction();
			for (Object object : obj) {
				session.delete(object);
			}	
			tx.commit();
			session.flush();
			session.close();
		}catch(Exception e) {
			reb = false;
		}
		return reb;
	}
	public static boolean update(Object obj) {
		boolean reb = true;
		try {
			Session session = HibernateUtil.getSession();
			Transaction tx = session.beginTransaction();
			session.update(obj);
			tx.commit();
			session.flush();
			session.close();
		}catch(Exception e) {
			reb = false;
		}
		return reb;
	}
	public static boolean update(Object[] obj) {
		boolean reb = true;
		try {
			Session session = HibernateUtil.getSession();
			Transaction tx = session.beginTransaction();
			for (Object object : obj) {
				session.update(object);
			}	
			tx.commit();
			session.flush();
			session.close();
		}catch(Exception e) {
			reb = false;
		}
		return reb;
	}
}
